package com.cjj.design_model;

import java.security.Signature;

/**
 * 保证JVM种有一个实例
 */
class Singleton{

    private static Singleton singleton;

    /**
     * 构造方法私有化，反射也不能进行实例化
     */
    private Singleton(){

    }

    /**
     * 饿汉式单例是线程不安全的，所需需要使用到同步代码块来保证线程安全
     * @return
     */
    static public Singleton getSingleton(){
        if(singleton == null){
            synchronized (Singleton.class){
                singleton = new Singleton();
            }
        }
        return singleton;
    }
}

/**
 * @ClassName SingletonTest_1
 * @Date 2021/07/28 16:34:25
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 * 懒汉式设计模式
 */
public class SingletonTest_1 {

    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getSingleton();
        Singleton singleton2 = Singleton.getSingleton();
        System.out.println(singleton1 == singleton2);
    }

}
